package com.vue.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vue.pojo.Book;
import com.vue.service.BookService;
import com.vue.service.CateGoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalTime;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;
    @Autowired
    private CateGoryService cateGoryService;

    @RequestMapping("/showw")
    public ResponseEntity<Page<Book>> show(Integer pagenumber, Integer pagesize, String name, String number) {
        Page<Book> findll = bookService.findll(pagenumber, pagesize, name, number);
        for (int i = 0; i < findll.getRecords().size(); i++) {
            findll.getRecords().get(i).setCategoryID(cateGoryService.getById(findll.getRecords().get(i).getCategoryID()).getName());
        }
        return new ResponseEntity<>(findll, HttpStatus.OK);
    }

    @RequestMapping("/add")
    public void add(String name, String author, String isbn, String info, String image, String categoryID, Integer wordNum,
                    Integer format, String publisher, Double price, String publishDate) throws ParseException {
        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setCategoryID(categoryID);
        book.setFormat(format);
        book.setImage(image);
        book.setInfo(info);
        book.setPrice(price);
        book.setIsbn(isbn);
        book.setWordNum(wordNum);
        book.setPayNumber(0);
        book.setPublisher(publisher);
        book.setPublishDate(LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond());
        bookService.addOne(book);
    }
    @RequestMapping("/del")
    public ResponseEntity<String> del(Integer id){
        int i = bookService.delOne(id);
        if(i==1){
            return new ResponseEntity<>("删除成功",HttpStatus.OK);
        }else{
            return new ResponseEntity<>("删除失败",HttpStatus.NOT_FOUND);
        }
    }
    @RequestMapping("/up")
    public ResponseEntity<String> up(Integer id, String isbn, String name, String categoryID,  Integer wordNum, Integer format, String author, String publisher, String publishDate, Double price, Integer payNumber){
        Book book=new Book();
        book.setId(id);
        book.setIsbn(isbn);
        book.setName(name);
        book.setWordNum(wordNum);
        book.setFormat(format);
        book.setAuthor(author);
        book.setPublishDate(publishDate);
        book.setPublisher(publisher);
        book.setPrice(price);
        book.setPayNumber(payNumber);
       book.setCategoryID( cateGoryService.fineByName(categoryID).getId()+"");
        int i = bookService.up(book);
        if(i==1){
            return new ResponseEntity<>("修改成功",HttpStatus.OK);
        }else{
            return new ResponseEntity<>("修改失败",HttpStatus.OK);
        }
    }
}
